<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
function appendNode( $parent, $child, $doc )
{
	if( $child == NULL)
	{
		return;
	}
	$newChild = NULL;
	if( $child-> nodeType == XML_TEXT_NODE )
	{
		$newChild = $doc-> createTextNode($child->nodeValue);
		//echo $newChild->nodeValue."\n";
	}
	else if( $child-> nodeType == XML_ELEMENT_NODE)
	{
		//echo $child->tagName."\n";
		$childTag = $child->tagName;
		$newChild = $doc-> createElement($childTag);
		
		foreach( $child->attributes as $attribute)
		{
			$newChild->setAttribute($attribute->name, $attribute->value);
		}
		
		foreach ( $child->childNodes as $childsChild)
		{
			appendNode( $newChild, $childsChild, $doc);
		}
	}
	if( $newChild != NULL)
	{
		$parent-> appendChild( $newChild);
	}
}

function extract_scripts($xml, $parent)
{
	$script = "";
	if ( $xml->nodeType == XML_ELEMENT_NODE) // only for element node
	{
		if( $parent != NULL && strtolower($xml->nodeName == "script" ) )
		{
			$script .= $xml->nodeValue;
			$parent->removeChild($xml);
		}
		else
		{
			foreach ($xml->childNodes as $child)
			{
				$script .= extract_scripts($child, $xml);
			}
		}
	}
	return $script;
}

function serialize_node($xml)
{
	$nodeText = "";
	
	if ($xml->nodeType == XML_TEXT_NODE) { //for text node
		$nodeText = $xml->nodeValue;
	}
	else if ( $xml->nodeType == XML_ELEMENT_NODE) // for element node
	{
		$nodeName = $xml->nodeName;
		
		$nodeText = "<" . $nodeName;
		
		// add attributes
		foreach( $xml->attributes as $attri)
		{
			$nodeText .= " " . $attri->name . "=" . "\"" . $attri->value . "\" ";
		}
		
		$nodeText .= ">";
		
		// add childs
	
		foreach ($xml->childNodes as $child)
		{
			$childText = serialize_node($child);
			$nodeText .= $childText;
		}
		
		$nodeText .= "</" . $nodeName . ">";
		
	}
	return $nodeText;	
}
?>